package com.supermap.business.wxtoken;

import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.util.HttpRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author aqua
 * @description
 * @date 2018年10月30 上午 9:56
 * @email 1301584033@qq.com
 */
@Controller
@CrossOrigin
@RequestMapping("/wxtoken")
public class wxController {

    @Value("${weixin.appid}")
    private String AppId;
    @Value("${weixin.secret}")
    private String secret;

    @RequestMapping("/verify_wx_token")
    @ResponseBody
    public String verifyWXToken(HttpServletRequest request) throws AesException {
        String msgSignature = request.getParameter("signature");
        String msgTimestamp = request.getParameter("timestamp");
        String msgNonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        if (WXPublicUtils.verifyUrl(msgSignature, msgTimestamp, msgNonce)) {
            return echostr;
        }
        return null;
    }
    @GetMapping("/getAccessToken")
    @ResponseBody
    public JSONObject getAccessToken() {
        String grant_type = "client_credential";//获取access_token填写client_credential
        //这个url链接地址和参数皆不能变
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+AppId+"&secret="+secret;
        String result = HttpRequest.httpURLConectionGET(url);
        return  JSONObject.parseObject(result);
    }

    @GetMapping("/getTicket")
    @ResponseBody
    public JSONObject getTicket(String access_token) {
        String grant_type = "client_credential";//获取access_token填写client_credential
        //这个url链接地址和参数皆不能变
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url链接和参数不能变
        String result = HttpRequest.httpURLConectionGET(url);
        JSONObject jsStr = JSONObject.parseObject(result);
        return  jsStr;
    }
    @GetMapping("/getSHA1ToStr")
    @ResponseBody
    public  String SHA1(String ticket) {
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
            digest.update(ticket.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    @GetMapping("/getSHA1")
    @ResponseBody
    public Map<String,Object> getSHA1(String url){
        Map<String,Object> parm=new HashMap<>();
        parm.put("expires_in",7200);
        JSONObject jsostr=getAccessToken();
        String access_token=jsostr.get("access_token").toString();
        jsostr=getTicket(access_token);
        String noncestr = UUID.randomUUID().toString().replace("-","").substring(0,16);
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        String str = "jsapi_ticket=" + jsostr.get("ticket").toString() +
                "&noncestr=" + noncestr +
                "&timestamp=" + timestamp +
                "&url=" + url;
        //sha1加密
        String signature = SHA1(str);
        parm.put("jsapiTicket",jsostr.get("ticket").toString());
        parm.put("noncestr",noncestr);
        parm.put("timestamp",timestamp);
        parm.put("signature",signature);
        parm.put("str",str);
        return parm;

    }










}
